/**
 * Copyright (c) 2013 An Yaming,  All Rights Reserved
 */
package so.shanku.essential.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.text.TextUtils;
import aym.util.getservicesdata.GetServicesDataUtil;
import net.tsz.afinal.annotation.view.ViewInject;
import so.shanku.essential.R;
import so.shanku.essential.activity.base.BaseUIActivity;
import so.shanku.essential.adapter.PaymentListAdapter;
import so.shanku.essential.getdata.GetDataConfing;
import so.shanku.essential.getdata.GetDataQueue;
import so.shanku.essential.getdata.JsonKeys;
import so.shanku.essential.getdata.ShowGetDataError;
import so.shanku.essential.utils.ExtraKeys;
import so.shanku.essential.utils.LogUtil;
import so.shanku.essential.view.GridViewNoScroll;
import so.shanku.essential.view.ListViewNoScroll;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import aym.util.getservicesdata.GetServicesDataQueue;
import aym.util.getservicesdata.GetServicesDataUtil.IGetServicesDataCallBack;
import aym.util.json.JsonMap;
import aym.util.json.JsonParseHelper;

/**
 * @author tanghuan
 * @ClassName: ShoppingZhifuPeisongWayActivity
 * @Description: TODO(支付与配送方式类)
 * @date 2014年12月3日 下午7:53:40
 */
public class ShoppingZhifuPeisongWayActivity extends BaseUIActivity {
	/**
	 * 支付方式
	 */
	@ViewInject(id = R.id.shopping_addorder_gvns_payment, itemClick = "list_mlv")
	private ListViewNoScroll gvns_payment;
	/**
	 * 配送方式
	 */
	@ViewInject(id = R.id.shopping_addorder_gvns_sendmethod, itemClick = "list_mlv")
	private GridViewNoScroll gvns_sendmethod;
	/**
	 * 配送方式的选择的数据源
	 */
	private List<JsonMap<String, Object>> data_peisong;
	/**
	 * 确认按钮
	 */
	@ViewInject(id = R.id.u_i_u_bt_config, click = "config")
	private Button u_bt_config;

	private PaymentListAdapter adapter;


	/**
	 * 选中的支付方式
	 */
	private int selectid = 0;
	private String payid = "";
	private String orderNum;
	private String payName;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_shopping_zhifu_peisong_way);
		// setCenter_title(R.string.shopping_zhifu_peisong_way_text_title);
		setCenter_title(R.string.shopping_zhifu_peisong_way_text_payway1);
		orderNum = getIntent().getStringExtra(ExtraKeys.Key_Msg2);

		getData_zhifupeisong();

//
//		List<JsonMap<String, Object>> ps = new ArrayList<JsonMap<String, Object>>();
//		JsonMap<String, Object> m = new JsonMap<String, Object>();
//		m.put(JsonKeys.Key_ObjName,
//				getString(R.string.shopping_zhifu_peisong_way_text_def));
//		m.put("IsDefault", true);
//		ps.add(m);
//		setAdapter_peisong(ps);

	}

	/**
	 * @Title: list_mlv
	 * @Description: TODO(方法的作用：列表点击事件)
	 */
	public void list_mlv(AdapterView<?> parent, View view, int position, long id) {
		selectid = position;
		adapter.setSelected(position);
	}


	/**
	 * 为支付选择框设定数据源
	 *
	 * @param data
	 */
	private void setAdapter_zhifu(List<JsonMap<String, Object>> data) {
		if (data != null) {
			adapter = new PaymentListAdapter(this, data);
			gvns_payment.setAdapter(adapter);
			for (int i = 0; i < data.size(); i++) {
				if (data.get(i).getBoolean("IsDefault")) {
					adapter.setSelected(i);
				}
			}

		}
	}

	/**
	 * 为支付选择框设定数据源
	 *
	 * @param data
	 */
	private void setAdapter_peisong(List<JsonMap<String, Object>> data) {
		Log.i(TAG, "data.size()=" + data.size());
		if (data != null) {
			// adapter = new PaymentListAdapter(this, data,
			// R.layout.item_shopping_zhifu_peisong_way,
			// new String[] { JsonKeys.Key_ObjName },
			// new int[] { R.id.item_shopping_addorder_gvns_payment }, 0);
			// gvns_sendmethod.setAdapter(adapter);
			// for (int i = 0; i < data.size(); i++) {
			// if (data.get(i).getBoolean("IsDefault")) {
			// adapter.setSelected(i);
			// }
			// ;
			// }

			// 修改显示方式
			// String[] peisong = new String[data.size()];
			// for (int i = 0; i < data.size(); i++) {
			// peisong[i] = data.get(i).getStringNoNull(JsonKeys.Key_ObjName);
			// }
			// ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
			// android.R.layout.simple_spinner_item, peisong);
			// adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			// gvns_sendmethod.setPromptId(R.string.shopping_zhifu_peisong_way_text_selete_sendway);
			// gvns_sendmethod.setAdapter(adapter);
		}
	}

	/**
	 * 获取服务器支付和配送新
	 */
	private void getData_zhifupeisong() {
		loadingToast.show();
		GetDataQueue queue = new GetDataQueue();
		queue.setActionName(GetDataConfing.Action_shoppingGetPayType);
		queue.setParams(null);
		queue.setWhat(GetDataConfing.What_shoppingGetPayType);
		queue.setCallBack(callBack);
		getDataUtil.getData(queue);
	}


	public void config(View v) {
		if (selectid >= 0) {
			JsonMap<String, Object> selectInfo = data_peisong.get(selectid);
			payName=selectInfo.getStringNoNull(JsonKeys.Key_ObjName);
			payid=selectInfo.getStringNoNull(JsonKeys.Key_ObjId);
			if(!TextUtils.isEmpty(orderNum)) {
				loadingToast.show();

				Map<String, Object> params = new HashMap<String, Object>();
				params.put(GetDataQueue.Params_key, GetDataConfing.Key_Str);
				params.put("orderNum", orderNum);
				params.put("PayTypeId",payid);
				GetDataQueue queue = new GetDataQueue();
				queue.setCallBack(callBack);
				queue.setActionName(GetDataConfing.Action_GetOrderByOrderNumUpdatePayType);
				queue.setWhat(GetDataConfing.What_GetOrderByOrderNumUpdatePayType);
				queue.setParamsNoJson(params);
				getDataUtil.getData(queue);
			} else {
				setPayConfigSuccess();
			}

		}

	}

	/**
	 * 获取服务器数据的返回信息
	 */
	private GetServicesDataUtil.IGetServicesDataCallBack callBack = new GetServicesDataUtil.IGetServicesDataCallBack() {
		@Override
		public void onLoaded(GetServicesDataQueue entity) {
			if (entity.isOk()) {
				if (ShowGetDataError.isOkAndCodeIsNot1(mContext,
						entity.getInfo())) {
					if (entity.what == GetDataConfing.What_GetOrderByOrderNumUpdatePayType) {
						setPayConfigSuccess();
					} else if (entity.what == GetDataConfing.What_shoppingGetPayType) {
						data_peisong = JsonParseHelper.getJsonMap_List_JsonMap(
								entity.getInfo(), JsonKeys.Key_Info);
						setAdapter_zhifu(JsonParseHelper
								.getJsonMap_List_JsonMap(entity.getInfo(),
										JsonKeys.Key_Info));
					}
				}
			} else {
				ShowGetDataError.showNetError(mContext);
			}
			loadingToast.dismiss();
		}
	};

	private void setPayConfigSuccess() {

		Intent intent = new Intent();
		intent.putExtra(ExtraKeys.Key_Msg1,payName);
		intent.putExtra(ExtraKeys.Key_Msg2,payid);
		intent.putExtra(ExtraKeys.Key_Msg3, selectid);
		LogUtil.d(TAG, "selected pay id:" + payid);

		setResult(RESULT_OK, intent);
		finish();
	}

}
